import {
  _decorator,
  CCInteger,
  Component,
  Node,
  Animation,
  Collider2D,
  Contact2DType,
  IPhysics2DContact,
  Sprite,
} from 'cc'
const { ccclass, property } = _decorator

export enum RewardType {
  TwoShoot = 0,
  Bomb = 1,
}

@ccclass('Reward')
export class Reward extends Component {
  // 奖励类型
  @property({ type: CCInteger })
  type: RewardType = RewardType.TwoShoot

  @property({ type: CCInteger })
  speed: number = 90

  private _collider: Collider2D = null

  start() {
    this._collider = this.node.getComponent(Collider2D)
    if (this._collider) {
      this._collider.on(Contact2DType.BEGIN_CONTACT, this.onBegainContact, this)
    }
  }

  update(deltaTime: number) {
    const { position } = this.node
    this.node.setPosition(position.x, position.y - this.speed * deltaTime, position.z)
    if (position.y < -570) {
      this.node.destroy()
    }
  }
  onBegainContact(selfCollider: Collider2D) {
    selfCollider.enabled = false
    selfCollider.getComponent(Sprite).enabled = false
    // this.scheduleOnce(() => {
    //   this.node.destroy()
    // }, 0.1)
  }
  protected onDestroy(): void {
    if (this._collider) {
      this._collider.off(Contact2DType.BEGIN_CONTACT, this.onBegainContact, this)
    }
  }
}
